from . import admin
from app import db
from app.models import Setting
from flask import render_template, request
from app.some_func import wapper, fail_api, success_api, table_api
from app.schemas import SettingOutSchema
import html
from datetime import datetime
# 设置管理
@admin.get('/setting/')
@wapper
def setting_main():
    return render_template('admin/setting/main.html')
# 表格数据
@admin.get('/setting/data')
@wapper
def setting_table():
    setting = request.args.get('setting', type=str)
    filters = []
    if setting:
        filters.append(Setting.title.contains(setting))
    settings = Setting.query.filter(*filters).paginate(page=request.args.get('page', type=int),per_page=request.args.get('limit', type=int),error_out=False)
    return table_api(data=SettingOutSchema(many=True).dump(settings), count=settings.total)
# 增加设置
@admin.get('/setting/add')
@wapper
def setting_add():
    return render_template('admin/setting/add.html')
# 保存设置
@admin.post('/setting/save')
@wapper
def setting_save():
    if not request.json.get('title') or not request.json.get('subtitle')  or not request.json.get('discription') or not request.json.get('about') or not request.json.get('keywords') or not request.json.get('notice'):
        return fail_api(msg="所有内容不得为空")
    title = request.json.get('title')
    subtitle = request.json.get('subtitle')
    discription = request.json.get('discription')
    about = html.unescape(request.json.get('about'))
    keywords = request.json.get('keywords')
    hot_article = request.json.get('hot_article')
    notice = request.json.get('notice')
    setting = Setting(title=title, subtitle=subtitle, discription=discription, about=about, keywords=keywords, notice=notice, hot_article=hot_article)
    db.session.add(setting)
    db.session.commit()
    return success_api(msg="增加成功")
# 删除设置
@admin.delete('/setting/remove/<int:id>')
@wapper
def setting_delete(id):
    setting = Setting.query.filter_by(id=id).first()
    res = Setting.query.filter_by(id=id).delete()
    db.session.commit()
    if not res:
        return fail_api(msg="删除失败")
    return success_api(msg="删除成功")
# 编辑设置
@admin.get('/setting/edit/<int:id>')
@wapper
def setting_edit(id):
    setting = Setting.query.filter_by(id=id).first()
    return render_template('admin/setting/edit.html', setting=setting)
# 更新设置
@admin.put('/setting/update')
@wapper
def setting_update():
    if not request.json.get('title') or not request.json.get('subtitle') or not request.json.get('discription') or not request.json.get('about') or not request.json.get('keywords') or not request.json.get('notice'):
        return fail_api(msg="所有内容不得为空")
    id = request.json.get('id')
    title = request.json.get('title')
    subtitle = request.json.get('subtitle')
    discription = request.json.get('discription')
    about = html.unescape(request.json.get('about'))
    keywords = request.json.get('keywords')
    hot_article = request.json.get('hot_article')
    notice = request.json.get('notice')
    Setting.query.filter_by(id=id).update({'title':title, 'subtitle':subtitle, 'discription':discription, 'about':about, 'keywords':keywords, 'notice':notice, 'hot_article':hot_article})
    db.session.commit()
    return success_api(msg="更新成功")